<?php /* УТФ-8:fix */

class SPImodel__profiles__manage extends SPImodel__profiles
{
    public function select( $permission_id, $filter_search = null )
    {
        $permission_id      = (int) $permission_id;
        $limit              = (int) $limit ;
        $filter_search      = trim( $filter_search );

        $store = SPIstore::I();

        $query =    " SELECT * "
               .      " FROM ".SPIcfg::$store['table:profiles']
               .     " WHERE TRUE "
               . ( $permission_id ? $this->qfield_permission( 'permissions', $permission_id ) : '' )
               . ( $filter_search
                 ? '     AND (  ' . $store->prepare_search_query( 'id', $filter_search )
                 . '         OR ' . $store->prepare_search_query( 'fname', $filter_search )
                 . '         OR ' . $store->prepare_search_query( 'lname', $filter_search )
                 . '         OR ' . $store->prepare_search_query( 'email', $filter_search )
                 . '         OR ' . $store->prepare_search_query( 'description', $filter_search )
                 . '         ) '
                 : ''
                 )
               . " ORDER BY permissions, id DESC "
               ;

        return $store->query( $query );
    }

    public function get_by_id( $item_id )
    {
        $query =    " SELECT * "
               .      " FROM ".SPIcfg::$store['table:profiles'] . " AS prof "
               .     " WHERE id=" . (int) $item_id
               ;

        return SPIstore::I()->query( $query )->fetch_and_close();
    }

    public function set_activity( $set_activity_value, $item_id )
    {
        return SPIstore::I()->modify(
            SPIcfg::$store['table:profiles'],
            array( 'workstatus' => ($set_activity_value == 'on' ? 1 : 0 ) ),
            array( 'id' => $item_id )
        );
    }

    public function insert( $fields )
    {
        $fields['pid_profile_addby'] = SPI::I('Iam')->get_id();

        $result = SPIstore::I()->insert(
            SPIcfg::$store['table:profiles'],
            $fields,
            null,
            array( 'id' => SPIcfg::$store['sequence:global'] )
        );

        return $result === false ? false : $result['id'];
    }

    public function modify( $item_id, $fields )
    {
        return SPIstore::I()->modify(
            SPIcfg::$store['table:profiles'],
            $fields,
            array( 'id' => $item_id )
        );
    }

    public function delete( $item_id )
    {
        return SPIstore::I()->delete(
            SPIcfg::$store['table:profiles'],
            array( 'id' => $item_id )
        );
    }

    public function get_by_email( $email )
    {
        $query =    " SELECT * "
               .      " FROM ".SPIcfg::$store['table:profiles']
               .     " WHERE LOWER(email) = LOWER('" . addslashes( $email ) . "') "
               #       " AND workstatus=1 "
               ;

        return SPIstore::I()->query( $query )->fetch_and_close();
    }

    public function cronfix__avatar_flag()
    {
        $query =   " UPDATE ".SPIcfg::$store['table:profiles']." AS p "
               .      " SET is_has_avatar = 1 "
               .    " WHERE EXISTS( SELECT true FROM ".SPIcfg::$store['table:media']." AS m WHERE m.pid = p.id ) "
               .      " AND is_has_avatar = 0 "
               ;
        SPIstore::I()->query( $query )->close();

        $query =   " UPDATE ".SPIcfg::$store['table:profiles']." AS p "
               .      " SET is_has_avatar = 0 "
               .    " WHERE NOT EXISTS( SELECT true FROM ".SPIcfg::$store['table:media']." AS m WHERE m.pid = p.id ) "
               .      " AND is_has_avatar = 1 "
               ;
        SPIstore::I()->query( $query )->close();

        return true;
    }

    public function reset_order_indexes( $permissions )
    {
        return SPIstore::I()->modify(
            SPIcfg::$store['table:profiles'],
            array( 'order_index'=>-1 ),
            array( 'permissions' => $permissions )
        );
    }
}
